/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.agent.service;

import cn.ac.iie.agent.tools.FtpTools1;
import cn.ac.iie.agent.tools.PropsFiles;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author dell
 */
public class FtpUploadDataFilesThread implements Runnable {

    private static Logger logger = Logger.getLogger(FtpUploadDataFilesThread.class);
    
    String subjobid;
    String jsonFileName;
    String localPath;
    List<Map<String, String>> dataFileList;
    Map<String, String> ftp;
    List<String[]> errMsglist;
    List<String> execute;

    public FtpUploadDataFilesThread(String subjobid, String jsonFileName, String localPath, List<Map<String, String>> dataFileList, Map<String, String> ftp, List<String[]> errMsglist, List<String> execute) {
        this.subjobid = subjobid;
        this.jsonFileName = jsonFileName;
        this.localPath = localPath;
        this.dataFileList = dataFileList;
        this.ftp = ftp;
        this.errMsglist = errMsglist;
        this.execute = execute;
    }

    @Override
    public void run() {
        PropsFiles ps = new PropsFiles();
String ftpPathPrefix = ps.getValue("uploadType");
        logger.info(subjobid + " begin to upload datafiles. And the ftpprefix is: " + ftpPathPrefix);
        String filename = "";
        String localdata = localPath + "Data/";
        String localctrl = localPath + "Ctrl/";
        logger.info(subjobid + " the localCtrlPath is: " + localctrl + " and the localDataPath is: " + localdata);
//        String ftpDatapath = "/" + PropsFiles.getValue("uploadType") + "/" + ftp.get("namespace") + "/Data/";
//        String ftpCtrlpath = "/" + PropsFiles.getValue("uploadType") + "/" + ftp.get("namespace") + "/Ctrl/";
        String ftpDatapath = "/" + ftp.get("namespace") + "/Data/";
        String ftpCtrlpath = "/" + ftp.get("namespace") + "/Ctrl/";
        logger.info(subjobid + " the ftpCtrlPath is: " + ftpCtrlpath + " and the ftpDataPath is: " + ftpDatapath);

        String ip = (String) ftp.get("ip");
        String portStr = (String) ftp.get("port");
        int port = Integer.parseInt(portStr);
        String username = (String) ftp.get("username");
        String password = (String) ftp.get("password");
        String namespace = (String) ftp.get("namespace");
        logger.info(subjobid + " the ftp ip: " + ip + " ,the port is: " + port + " ,the username is: " + username + " ,the password is: " + password + " ,the namespace is: " + namespace);

        String returnFlag = "";//所有data目录下数据文件上传flag
        logger.info(subjobid + " *******************the json file: " + jsonFileName + " has " + dataFileList.size() + " files.");
        for (int i = 0; i < dataFileList.size(); i++) {
            Map<String, String> str = dataFileList.get(i);
            filename = str.get("name");
            String date = filename.substring(1, filename.lastIndexOf("/"));
            String realName = filename.substring(filename.lastIndexOf("/") + 1, filename.length());
            logger.info(subjobid + " upload the data file, and the data file is: " + filename);

            boolean uploadFile = FtpTools1.upload(ip, port, username, password, localdata + date + "/" + realName, ftpPathPrefix, ftpDatapath + date, realName);
            logger.info(subjobid + " ip: " + ip + " upload the data file " + filename + " return " + uploadFile);
            if (!uploadFile) {
                String[] message = {filename, "1"};
                errMsglist.add(message);
            }
            returnFlag = returnFlag + uploadFile + ",";
        }
        //上传json文件
        if (!returnFlag.contains("false")) {
            logger.info(subjobid + " upload the json file, and the data file is: " + jsonFileName + " ,the localPath is " + localctrl + jsonFileName + " ,the ftpPath is " + ftpCtrlpath);
            boolean upload = FtpTools1.upload(ip, port, username, password, localctrl + jsonFileName, ftpPathPrefix, ftpCtrlpath, jsonFileName);
            logger.info(subjobid + " uplaod the json file return " + upload);
            //.ok
            FtpTools1.okFile(ip, port, username, password, ftpPathPrefix + ftpCtrlpath, jsonFileName);
            execute.add("true");
        } else {
            execute.add("false");
        }
    }
}
